package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.housediyadmin.events.treeEvents.AddAreaLevelEvent;
	import com.housediyadmin.events.treeEvents.AddGoodsLevelEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.vo.Building;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.Text;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class EditTreeMediator extends Mediator
	{
		[Inject]
		public var view:EditTreeView;
		[Inject]
		public var buildingTreeModel:AreaTreeModel;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;
		
		override public function onRegister():void
		{
			view.nextBtn.addEventListener(MouseEvent.CLICK,next);
			view.addNodeCanvas.addEventListener(FlexEvent.CREATION_COMPLETE,addNodeCanvasComplete);
			view.addRootCanvas.addEventListener(FlexEvent.CREATION_COMPLETE,addRootCanvasComplete);
			view.addEventListener(CloseEvent.CLOSE,closeTitleWindowEve);
		}
		
		private function closeTitleWindowEve(e:CloseEvent):void
		{
			closeTitleWindow();
		}
		private function closeTitleWindow():void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
		
		private function next(e:MouseEvent):void
		{
			if(view.addNodeRB.selected)
			{
				view.addViewStack.selectedChild=view.addNodeCanvas;
			}
			else if(view.addRootRB.selected)
			{
				view.addViewStack.selectedChild=view.addRootCanvas;
			}
		}
		
		private function addNodeCanvasComplete(e:FlexEvent):void
		{
			view.previousBtn_addNodeCanvas.addEventListener(MouseEvent.CLICK,previous);
			view.okBtn_addNodeCanvas.addEventListener(MouseEvent.CLICK,addNodeEve);
		}
		private function addRootCanvasComplete(e:FlexEvent):void
		{
			view.previousBtn_addRootCanvas.addEventListener(MouseEvent.CLICK,previous);
			view.okBtn_addRootCanvas.addEventListener(MouseEvent.CLICK,addRootEve);
		}
		
		private function previous(e:MouseEvent):void
		{
			view.addViewStack.selectedChild=view.selectCanvas;	
		}
		private function addRootEve(e:MouseEvent):void
		{
			addRoot();
			closeTitleWindow();
		}
		
		private function addNodeEve(e:MouseEvent):void
		{
			addNode();			
			closeTitleWindow();
		}
		
		private function addRoot():void
		{
			var typeName:String = view.rootNameTxt.text;
			var typeParentID:int=0;
			var typeLevel:int=1;
			switch(view.type)
			{
				case "BUILDING":
					dispatch(new AddAreaLevelEvent(AddAreaLevelEvent.ADD_AREA_LEVEL, typeName, typeParentID, typeLevel));
					break;
				case "GOODS":
					dispatch(new AddGoodsLevelEvent(AddGoodsLevelEvent.ADD_GOODS_LEVEL, typeName, typeParentID, typeLevel));
					break;
			}
		}
		private function addNode():void
		{
			var xml:XML=view.parameter;
			var typeName:String = view.nodeNameTxt.text;
			var typeParentID:int= xml.@typeID;
			var typeLevel:int= int(xml.@typeLevel) +1;
			switch(view.type)
			{
				case "BUILDING":
					dispatch(new AddAreaLevelEvent(AddAreaLevelEvent.ADD_AREA_LEVEL, typeName, typeParentID, typeLevel));
					break;
				case "GOODS":
					dispatch(new AddGoodsLevelEvent(AddGoodsLevelEvent.ADD_GOODS_LEVEL, typeName, typeParentID, typeLevel));
					break;
			}
			
		}
	}
}